//
//  whoWillBeAbleToCommand.m
//  SQLiteTesting
//
//  Created by kitinat on 2/13/13.
//  Copyright (c) 2013 EGA. All rights reserved.
//
#import "EGAAppDelegate.h"
#import "whoWillBeAbleToCommand.h"
#import "forwardCommandVC.h"
#import <QuartzCore/QuartzCore.h>
#import "EGAUtility.h"

@interface whoWillBeAbleToCommand ()

@end


@implementation whoWillBeAbleToCommand

@synthesize DocID;
@synthesize EmpID;
@synthesize tblWhoToCommand;

NSData * data;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self dismissKeyboard];
    
    [self setCornerTable];
    
    
//    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http:%@WhoYouWillBeAbleToCommand.ashx?EmpID=%@&DocID=%@",serviceUrl,EmpID,DocID]];
//    data = [[NSData alloc] initWithContentsOfURL:URL];

    
    /////////////////new
    EGAUtility * Util = [[EGAUtility alloc]init];
    NSString *jsonURL=[NSString stringWithFormat:@"WhoYouWillBeAbleToCommand.ashx&EmpID=%@&DocID=%@",EmpID,DocID];
    data=[Util getDataFromService:jsonURL];
    /////////////////new
    NSArray * whoToCommandArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    [tblWhoToCommand reloadData];
}

#pragma dismisskeyboard
- (void)dismissKeyboard {
    [self globalResignFirstResponder];
}
- (void) globalResignFirstResponder {
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    for (UIView * view in [window subviews]){
        [self globalResignFirstResponderRec:view];
    }
}
- (void) globalResignFirstResponderRec:(UIView*) view {
    if ([view respondsToSelector:@selector(resignFirstResponder)]){
        [view resignFirstResponder];
    }
    for (UIView * subview in [view subviews]){
        [self globalResignFirstResponderRec:subview];
    }
}





-(void)setCornerTable
{
    self.tblWhoToCommand.layer.borderColor =[UIColor colorWithRed:0/255.0 green: 100/255.0 blue:0/255.0 alpha:1].CGColor;
    self.tblWhoToCommand.layer.borderWidth =  0.5;
    self.tblWhoToCommand.layer.cornerRadius=5;
}

- (NSInteger) tableView : (UITableView *) tableView numberOfRowsInSection : (NSInteger) section
{
    NSArray * whoToCommandArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    return [whoToCommandArray count];
}

- (UITableViewCell *) tableView : (UITableView *) tableView cellForRowAtIndexPath :(NSIndexPath *) indexPath
{
    
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier : MyIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle : UITableViewCellStyleSubtitle reuseIdentifier : MyIdentifier];
    }

    NSArray * whoToCommandArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSDictionary *info = [whoToCommandArray objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [info objectForKey:@"Fullname"];
    //cell.detailTextLabel.text = [info objectForKey:@"Doc_Register"];
    
    cell.textLabel.font = [UIFont systemFontOfSize:20];
    [cell.textLabel setTextColor:[UIColor darkGrayColor]];
    
    return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([_delegate respondsToSelector:@selector(choosenWhoWithID:choosenEmpName:)])
    {
        //call delegate method in forwardcommandVC with choosenEmpID
        NSArray * whoToCommandArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *info = [whoToCommandArray objectAtIndex:indexPath.row];
        NSString*strChoosenEmpID = [info objectForKey:@"ID"];
        NSString*strChoosenEmpName = [info objectForKey:@"Fullname"];

        [_delegate choosenWhoWithID:strChoosenEmpID choosenEmpName:strChoosenEmpName];
        //[self.view removeFromSuperview];
    }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
